using DaoHang.Data;
using DaoHang.Models;
using Microsoft.EntityFrameworkCore;

namespace DaoHang.Services
{
    /// <summary>
    /// 网站设置服务实现类
    /// </summary>
    public class SiteSettingsService : ISiteSettingsService
    {
        private readonly ApplicationDbContext _context;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context">数据库上下文</param>
        public SiteSettingsService(ApplicationDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 获取网站设置
        /// </summary>
        /// <returns>网站设置对象</returns>
        public async Task<SiteSettings> GetSiteSettingsAsync()
        {
            // 获取第一个设置记录，如果不存在则创建默认设置
            var settings = await _context.SiteSettings
                .OrderBy(s => s.Id)
                .FirstOrDefaultAsync();
            
            if (settings == null)
            {
                settings = new SiteSettings
                {
                    IcpNumber = "",
                    PoliceNumber = "",
                    AdminRoute = "admin",
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now
                };
                
                _context.SiteSettings.Add(settings);
                await _context.SaveChangesAsync();
            }
            
            return settings;
        }

        /// <summary>
        /// 保存网站设置
        /// </summary>
        /// <param name="settings">网站设置对象</param>
        /// <returns>是否保存成功</returns>
        public async Task<bool> SaveSiteSettingsAsync(SiteSettings settings)
        {
            try
            {
                var existingSettings = await _context.SiteSettings
                    .OrderBy(s => s.Id)
                    .FirstOrDefaultAsync();
                
                if (existingSettings == null)
                {
                    // 如果不存在设置记录，创建新的
                    settings.CreateTime = DateTime.Now;
                    settings.UpdateTime = DateTime.Now;
                    _context.SiteSettings.Add(settings);
                }
                else
                {
                    // 更新现有设置
                    existingSettings.IcpNumber = settings.IcpNumber;
                    existingSettings.PoliceNumber = settings.PoliceNumber;
                    existingSettings.SiteTitle = settings.SiteTitle;
                    existingSettings.SiteDescription = settings.SiteDescription;
                    existingSettings.SiteKeywords = settings.SiteKeywords;
                    existingSettings.CopyrightInfo = settings.CopyrightInfo;
                    existingSettings.LogoUrl = settings.LogoUrl;
                    existingSettings.FaviconUrl = settings.FaviconUrl;
                    existingSettings.ThemeColor = settings.ThemeColor;
                    existingSettings.AliCloudApiKey = settings.AliCloudApiKey;
                    // 不更新AliCloudApiSecret，保留原有值
                    existingSettings.AiModelName = settings.AiModelName;
                    existingSettings.ApiKey = settings.ApiKey;
                    existingSettings.AdminRoute = settings.AdminRoute;
                    existingSettings.UpdateTime = DateTime.Now;
                }
                
                await _context.SaveChangesAsync();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 更新后台管理路由
        /// </summary>
        /// <param name="adminRoute">新的管理路由</param>
        /// <returns>是否更新成功</returns>
        public async Task<bool> UpdateAdminRouteAsync(string adminRoute)
        {
            try
            {
                var settings = await GetSiteSettingsAsync();
                settings.AdminRoute = adminRoute;
                return await SaveSiteSettingsAsync(settings);
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 获取当前的后台管理路由
        /// </summary>
        /// <returns>管理路由</returns>
        public async Task<string> GetAdminRouteAsync()
        {
            var settings = await GetSiteSettingsAsync();
            return string.IsNullOrEmpty(settings.AdminRoute) ? "admin" : settings.AdminRoute;
        }
    }
} 